package org.lwxyz.service.shadowsocks;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.lwxyz.bean.shadowsocks.Authorize;
import org.lwxyz.bean.shadowsocks.AuthorizeResult;
import org.lwxyz.utils.JsonUtils;
import org.lwxyz.utils.SSHUtil;
import org.lwxyz.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author yuwei
 * @version 1.0-SNAPSHOT
 * @description
 * @date
 */
@Service
public class AuthorizeService {
    /**
     * 获取ss密码
     */
    private String pwd = "cat /root/.kiwivm-shadowsocks-password";

    /**
     * 获取ss端口号
     */
    private String serverPort = "cat /root/.kiwivm-shadowsocks-port";

    /**
     * 获取自定义的ss信息
     */
    private String customInfo = "cat /etc/shadowsocks.json";

    /**
     * 获取ss信息
     * @return
     * @throws IOException
     */
    public AuthorizeResult authorize() throws IOException {
        List<Authorize> authorizeList = getCustomAuthorize();
        // 先获取自定义的信息
        if (authorizeList != null) {
            AuthorizeResult authorizeResult = new AuthorizeResult(authorizeList);
            return authorizeResult;
        }
        authorizeList = Lists.newArrayList();
        // 取不到自定义的ss信息
        Authorize authorize = new Authorize();
        // ss密码
        String password = getPassword();
        // ss端口号
        String port = getPort();
        authorize.setPassword(password);
        authorize.setServerPort(port);
        authorize.setEncryption("AES-256-CFB");
        authorize.setProxyPort("1080");
        authorize.setRemark("随便填点什么，你甚至可以留空。");
        authorize.setServerIP("23.106.146.77");
        authorizeList.add(authorize);
        AuthorizeResult authorizeResult = new AuthorizeResult(authorizeList);
        return authorizeResult;
    }

    /**
     * 获取自定义的ss信息
     * @return
     * @throws IOException
     */
    public List<Authorize> getCustomAuthorize() throws IOException {
        String customInfoJson = execute(customInfo);
        Map<String,Object> jsonMap = JsonUtils.json2Map(customInfoJson);
        String server = (String) jsonMap.get("server");
        if (StringUtil.isBlank(server)) {
            return null;
        }
        Integer local_port = (Integer) jsonMap.get("local_port");
        JSON port_password = (JSON) jsonMap.get("port_password");
        String method = (String) jsonMap.get("method");
        Map<String,Object> passwordMap = JsonUtils.json2Map(port_password.toJSONString());
        Set<String> keySet = passwordMap.keySet();
        List<Authorize> authorizeList = Lists.newArrayList();
        for (String key: keySet) {
            Authorize authorize = new Authorize(server, key, (String) passwordMap.get(key), method, "", local_port.toString());
            authorizeList.add(authorize);
        }
        return authorizeList;
    }

    /**
     * 获取ss密码
     * @return
     */
    public String getPassword() throws IOException {
        return execute(pwd);
    }

    /**
     * 获取ss端口号
     * @return
     */
    public String getPort() throws IOException {
        return execute(serverPort);
    }

    /**
     * 执行linux命令，并获取回显
     * @param cmd
     * @return
     */
    public String execute(String cmd) throws IOException {
        SSHUtil.ssh("23.106.146.77", 22, "root" , "MfnmUwx66R6e");
        List<String> stringList = SSHUtil.sshResult(cmd);
        StringBuffer sb = new StringBuffer();
        for (String result : stringList) {
            sb.append(result);
        }
        return sb.toString();
    }
}
